package com.ChenYaRou.www.controller;

import com.ChenYaRou.www.constant.JsonBeanConstant;
import com.ChenYaRou.www.dao.DetailedUserDao;
import com.ChenYaRou.www.dao.UserDao;
import com.ChenYaRou.www.dao.impl.DetailedUserDaoImpl;
import com.ChenYaRou.www.dao.impl.UserDaoImpl;
import com.ChenYaRou.www.po.DetailedUser;
import com.ChenYaRou.www.po.JsonBeanMessage;
import com.ChenYaRou.www.po.Message;
import com.ChenYaRou.www.po.User;
import com.ChenYaRou.www.utils.GenerateSql;
import com.ChenYaRou.www.utils.JsonBean;
import com.ChenYaRou.www.utils.UpdateOrInsertOrSelectMoreAllBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.ChenYaRou.www.constant.Constant.DESPAIR;
import static com.ChenYaRou.www.constant.Constant.SHOW;

/**
 * @ClassName MessageServletController
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/1 18:32
 * @Version 1.0
 **/
public class MessageServletController {
    /**
     * 添加评论
     * @param message
     * @return
     */
    public String addMessage(Message message){
        return UpdateOrInsertOrSelectMoreAllBean.getInstance().insert(message);
    }

    /**
     * 获取店铺的所有评论
     * @param shopId
     * @return
     */
    public String getShopAllMessage(Integer shopId){

        if(shopId!=null){
            try {
                Message message=new Message();
                message.setShopId(shopId);
                message.setState(SHOW);
                List<Message> messages = UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(message);

                List<JsonBeanMessage> jsonBeanMessageList=new ArrayList<>();
                if(messages!=null && !messages.isEmpty()){

                    for (Message message1 : messages) {
                        UserDao userDao=new UserDaoImpl();
                        User userGet = userDao.select(message1.getSenderId());
                        DetailedUserDao detailedUserDao=new DetailedUserDaoImpl();
                        DetailedUser detailedUser = detailedUserDao.select(userGet.getId());
                        jsonBeanMessageList.add(
                                new JsonBeanMessage(message1.getId(),null,
                                        message1.getContent(),userGet.getName(),
                                        message1.getLeftTime(),message1.getLikes(),
                                        message1.getParentId(),message1.getSenderId(),detailedUser.getPicture()));
                    }
                }
                return JsonBean.operateList(JsonBeanConstant.NULL,jsonBeanMessageList);
            } catch (IllegalAccessException | SQLException | NoSuchFieldException | InstantiationException e) {
                e.printStackTrace();
            }
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }


    /**
     * 删除message 即获取被操作的message,将其自身的id和所有parentId为其id的message的state设为隐藏,达到伪删除
     * @param message
     * @return
     */
    public String deleteMessage(Message message){
        if(message!=null) {
            Message newMessage = new Message();
            newMessage.setState(DESPAIR);
            try {
                String deleteTheMessage= GenerateSql.getInstance().update(newMessage,message);
                Message messageParentIdIsId=new Message();
                messageParentIdIsId.setParentId(message.getId());
                String deleteSonMessages = GenerateSql.getInstance().update(newMessage,messageParentIdIsId);
                System.out.println(deleteTheMessage);
                System.out.println(deleteSonMessages);
                return UpdateOrInsertOrSelectMoreAllBean.getInstance().insertOrUpdateMore(deleteSonMessages,deleteTheMessage);
            } catch (IllegalAccessException  e) {
                e.printStackTrace();
            }
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }
}
